import QtQuick 2.15
import QtQuick.Controls 2.15
import Translation 1.0
import SvgImage 1.0
Item {
    id: root
    opacity: enabled ? 1 : 0.5

    signal checkSpinBoxValue()
    signal cusTextEdited()
    signal upTextEdited()
    signal downTextEdited()
    signal editFinished()
    signal entered()
    signal exited()

    property alias cusInputItem: inputItem
    property alias text: inputItem.text
    property alias _focus: inputItem.focus
    property alias valueTimer: valueTimer

    property alias upBtn: upBtn
    property alias downBtn: downBtn
    property bool  upDownBtnRelease: true

    property real _value
    property int _accuracy: 3
    property int _step: 1
    property real _from: -1000.000
    property real _to: 1000.000

    property string proText: ""

    readonly property url up_arrow_normal: "qrc:/image/black_up_arrow_normal.svg"
    readonly property url up_arrow_hover: "qrc:/image/black_up_arrow_hover.svg"
    readonly property url up_arrow_pressed: "qrc:/image/black_up_arrow_pressed.svg"
    readonly property url down_arrow_normal: "qrc:/image/black_down_arrow_normal.svg"
    readonly property url down_arrow_hover: "qrc:/image/black_down_arrow_hover.svg"
    readonly property url down_arrow_pressed: "qrc:/image/black_down_arrow_pressed.svg"

    function up() {

        _value += _step;
        if( _value > _to ){
            _value = _to
        }
        inputItem.text = textFromValue(_value);

        upTextEdited()
        checkSpinBoxValue();
        inputItem.focus = false
        editFinished()
    }

    function down() {

        _value -= _step;
        if( _value < _from ){
            _value = _from
        }
        inputItem.text = textFromValue(_value);

        downTextEdited()
        checkSpinBoxValue();
        inputItem.focus = false
        editFinished()
    }

    function valueFromText(text) {
        return parseFloat(text).toFixed(_accuracy);
    }

    function textFromValue(value) {
        return parseFloat(value).toFixed(_accuracy).toString();
    }

    function lostFocus(){

        if( inputItem.focus ) return

        if( text == "-" | text == "-." | text == "." | text == "" | text == "+" | text == "+." ) {
            text = proText
        }

        _value = valueFromText(text)
        if( _value > _to ) {
            text = textFromValue(_to)
        }else if( _value < _from ){
            text = textFromValue(_from)
        }

        text = parseFloat(text).toFixed(_accuracy)

        proText = text
        editFinished()
    }
    function inChangeFocus(){
        //console.log(" text focus change: ", inputItem.focus);
    }

    TextField {
        id: inputItem
        clip: true

        anchors.left: parent.left
        anchors.bottom: parent.bottom
        anchors.right: parent.right
        anchors.top: parent.top
        topPadding: 2
        bottomPadding: 2
        leftPadding: 6
        rightPadding: 10

        font.family: Translation.family
        font.styleName : Theme.font_family_regular
        color: Theme.font_color
        font.pixelSize: Theme.font_size_13
        selectByMouse: true
        selectedTextColor: "white"
        selectionColor: Theme.main_color
        horizontalAlignment: TextInput.AlignLeft
        verticalAlignment: TextInput.AlignVCenter

        validator: DoubleValidator{
            decimals: _accuracy
            bottom: _from
            top: _to
            notation: DoubleValidator.StandardNotation
        }

        background: Rectangle {
            id: inputItemBack
            color: "transparent"
            radius: 4
            smooth: true
            border.width: 1
            border.color: {
                if( inputItem.focus ) { return Theme.border_focus_color  }
                else if( inputItem.hovered ) { return Theme.border_hover_color  }
                else { Theme.border_normal_color }
            }
        }

        onTextChanged: {
            if( !(text == "-" | text == "-." | text == "." | text == "" | text == "+" | text == "+.") ) {
                proText = text
                _value = valueFromText(text)
                cusTextEdited()
            }
        }
        Keys.onReturnPressed: { focus = false }
        Keys.onEnterPressed: { focus = false }

        //onPressed: { focus = true }

        onFocusChanged: {
            lostFocus();
            inChangeFocus();
        }

        onHoveredChanged: {
            if( hovered ){
                root.entered()
            }
            else{
                root.exited()
            }
        }
    }

    Rectangle {
        anchors.right: parent.right
        anchors.top: parent.top
        color: "transparent"
        width: 20
        height: parent.height / 2

        SvgImage {
            id: upBtn
            anchors.fill: parent
            source: up_arrow_normal

            MouseArea {
                anchors.fill: parent
                hoverEnabled: true
                pressAndHoldInterval: 200
                onEntered: upBtn.source = up_arrow_hover
                onExited: upBtn.source = up_arrow_normal

                onPressed: {
                    upDownBtnRelease = false;
                    upBtn.source = up_arrow_pressed
                    up()
                }

                onPressAndHold: {
                    //up()
                    valueTimer.upup = true; valueTimer.start()
                }
                onReleased: {
                    upDownBtnRelease = true
                    upBtn.source = up_arrow_normal
                    valueTimer.stop()
                }
            }
        }
    }

    Rectangle {
        anchors.right: parent.right
        anchors.bottom: parent.bottom
        width: 20
        color: "transparent"
        height: parent.height  / 2

        SvgImage {
            id: downBtn
            anchors.fill: parent
            source: down_arrow_normal

            MouseArea {
                anchors.fill: parent
                hoverEnabled: true
                pressAndHoldInterval: 200
                onEntered: downBtn.source = down_arrow_hover
                onExited: downBtn.source = down_arrow_normal

                onPressed: {
                    upDownBtnRelease = false
                    downBtn.source = down_arrow_pressed
                    down()
                }

                onPressAndHold: { valueTimer.upup = false; valueTimer.start() }
                onReleased: {
                    upDownBtnRelease = true
                    downBtn.source = down_arrow_normal
                    valueTimer.stop()
                }
            }
        }
    }

    Timer {
        id: valueTimer
        interval: 100
        repeat: true
        running: false
        property bool upup: false
        onTriggered: {
            if( valueTimer.upup ){up() }
            else { down() }
        }
    }
}
